<template>
    <el-dialog
        :title="titleMap[state.mode]"
        v-model="state.visible"
        :width="500"
        destroy-on-close
        @closed="$emit('closed')"
    >
        <el-form
            :model="form"
            :rules="rules"
            :disabled="state.mode == 'show'"
            ref="dialogFormRef"
            label-width="100px"
            label-position="left"
        >
            <el-form-item label="角色名称" prop="label">
                <el-input v-model="form.label" clearable></el-input>
            </el-form-item>
            <el-form-item label="角色标识" prop="alias">
                <el-radio-group v-model="form.alias">
                    <el-radio label="super">超级管理员</el-radio>
                    <el-radio label="admin">管理员</el-radio>
                    <el-radio label="staff">员工</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="排序" prop="sort">
                <el-input-number
                    v-model="form.sort"
                    controls-position="right"
                    :min="1"
                    style="width: 100%"
                ></el-input-number>
            </el-form-item>
            <el-form-item label="是否有效" prop="status">
                <el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" clearable type="textarea"></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="state.visible = false">取 消</el-button>
            <el-button
                v-if="state.mode != 'show'"
                type="primary"
                :loading="state.isSaving"
                @click="submit()"
                >保 存</el-button
            >
        </template>
    </el-dialog>
</template>

<script lang="ts" setup>
import { reactive } from 'vue'
import type { RoleModel, TitleMapModel } from '@/views/setting/role/type.ts'
import setting from '@/api/modules/setting.ts'
import feedback from '@/utils/feedback.ts'

const emit = defineEmits(['success', 'closed'])

const titleMap = reactive<TitleMapModel>({
    add: '新增用户',
    edit: '编辑用户',
    show: '查看'
})

const form = reactive<RoleModel>({
    id: '',
    label: '',
    alias: '',
    sort: 1,
    status: '1',
    createTime: '',
    deleteTime: '',
    menuIds: '',
    updateTime: '',
    remark: '',
    deleteFlag: 0
})

const rules = reactive({
    sort: [{ required: true, message: '请输入排序', trigger: 'change' }],
    label: [{ required: true, message: '请输入角色名称' }],
    alias: [{ required: true, message: '请输入角色别名' }]
})

const state = reactive({
    mode: 'add',
    visible: false,
    isSaving: false
})

const dialogFormRef = ref()

defineExpose({
    open,
    setData
})

// 显示
function open(mode = 'add') {
    state.mode = mode
    state.visible = true
}

// 表单提交方法
async function submit() {
    await dialogFormRef.value!.validate()

    state.isSaving = true
    const res = await setting.role.edit.post(form, { await: 1 })
    state.isSaving = false
    if (res) {
        emit('success', form, state.mode)
        state.visible = false
        feedback.success('操作成功')
    }
}

// 表单注入数据
function setData(data: RoleModel) {
    Object.assign(form, data)
}
</script>

<style></style>
